<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://isofttime.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\we\model;

use think\Model;



class WeSceneQrcode extends Model
{

    protected $insert = ['status'=>1]; 
    // 根据scene_id,scene_str 获取二维码信息
    public function info($appid,$scene_id, $field = true)
    {
        
        if (!$scene_id) return false;
        $map['appid'] = $appid;
        if (is_numeric($scene_id)){
            $map['scene_id'] = $scene_id;
        }else{
            $map['scene_str'] = $scene_id;
        }
        
        return $this->field($field)->where($map)->find();
    }

    public function infoById($id, $field = true)
    {
        
        if (!$id) return false;
        $map['id'] = $id;
       
        return $this->field($field)->where($map)->find();
    }

     // 获取自增scene_id;
    public function autoScene_id($appid,$type=0)
    {
        if (!$appid) return false;
        $map['appid']=$appid;
        $map['qrcode_type']=$type;
        $max=$this->where($map)->max('scene_id');
        if ($type==0 and !$max) $max=320000;  //避免与永久二维码冲突
        if ($type==1 and !$max) $max=0;
        $scene_id=intval($max)+1;
       
        return $scene_id;
    }

    public function editData(){

      $data=input("post.");
      
      if (isset($data['id'])){
         $res = $this->allowField(true)->where('id',$data['id'])->update($data);
      }else{
         $qrcode=get_qrcode($data['appid'],$data['qrcode_type']);
         if (!is_array($qrcode)) die($qrcode);
         mylog($qrcode);
         $qrcode['aid']=session('aid');
         $qrcode['shopid']=$data['shopid'];
         $res = $this->allowField(true)->data($data)->save($qrcode);
      }
      
      return $res;
    }

   
     public function deskAdmin($data)
    {
       
        
        $map['shopid']=$admin['shopid'];
        $map['code']='DESK'.$data['before'];
       
        $res=db('WeSceneQrcode')->where($map)->update(['admin'=>$admin['id']]);
        if ($res) return $data['before'].'号桌绑定成功，管理员'.$admin['name'];
        $qrocde=db('WeSceneQrcode')->where($map)->find();
        $admin=model('admin/AdminUser')->info($qrocde['admin']);
        return '绑定失败，'.$data['before'].'号桌当前管理员'.$admin['name'];
       
    }

    

   
   
}